<template>
    <app-layout>
        <view class="head">
            <view v-for="(item, index) in list" :key="index">
                <app-shipping-address
		                @handleAddress="address"
                        :item="item"
                        :is_refund_address="is_refund_address"
                        :is_hide_default_btn="is_hide_default_btn == 0"
                        :destroy_url="destroy_url"
                ></app-shipping-address>
            </view>
        </view>
        <view class="app-buttons main-between">
            <!-- #ifdef MP-ALIPAY || H5 || APP-PLUS -->
            <app-button :background="manual_btn_bg" arrangement="row" @click="manual" type="important" round width="700">
                <text class="app-icon app-manual-icon"></text>
                <text class="app-text">手动添加</text>
            </app-button>
            <!-- #endif -->
            <!-- #ifndef MP-ALIPAY  || H5 || APP-PLUS-->
            <app-button :background="manual_btn_bg" arrangement="row" @click="manual" type="important" round width="346">
                <text class="app-icon app-manual-icon"></text>
                <text class="app-text">手动添加</text>
            </app-button>
            <app-button background="#08c303" arrangement="row" @click="getauto" type="important" round width="346">
                <text class="app-icon app-auth-icon"></text>
                <text class="app-text">自动获取</text>
            </app-button>
            <!-- #endif -->
	    </view>
    </app-layout>
</template>

<script>
    import appShippingAddress from '../../components/page-component/app-shipping-address/app-shipping-address.vue';

    export default {
        name: "myaddress",
	    components: {
            'app-shipping-address': appShippingAddress,
	    },
	    data() {
            return {
                page: 1,
                args: '',
                load: '',
                list: null,
                manual_btn_bg: '',
                is_refund_address: 0,
                is_hide_default_btn: 0,
                address_url: '',
                destroy_url: '',
            }
	    },
        onLoad: function(options) {
            this.is_hide_default_btn = options.is_hide_default_btn ? options.is_hide_default_btn : 0;
            this.is_refund_address = options.is_refund_address ? options.is_refund_address : 0;

            this.manual_btn_bg = options.manual_btn_bg ? options.manual_btn_bg:`#ff4544`;

            if (this.is_refund_address > 0) {
                this.address_url = this.$api.app_admin.refund_address;
                this.destroy_url = this.$api.app_admin.address_destroy;
                uni.setNavigationBarTitle({
                    title: '退货地址'
                })
            } else {
                this.address_url = this.$api.user.address;
                this.destroy_url = this.$api.user.address_destroy;
            }

            //#ifdef MP-BAIDU
            this.init();
            //#endif
        },
        onShow: function () {
            this.init();
        },
        /**
         * 页面上拉触底事件的处理函数
         */
        onReachBottom: function () {
            const self = this;
            if (self.args || self.load)
                return;
            self.load = true;
            let page = self.page + 1;
            this.$request({
                url: self.$api.user.address,
                data: {
                    page: page,
                }
            }).then(info => {
                if (info.code === 0) {
                    [self.page, self.args, self.list] = [page, info.data.list.length === 0, self.list.concat(info.data.list)];
                }
                self.load = false;
            });
        },
	    methods: {
            init() {
                const self = this;
                if (!self.address_url) return;

                uni.showLoading({
                    title: '加载中'
                });
                self.page = 1;
                self.$request({
                    url: self.address_url,
                }).then(info => {
                    uni.hideLoading();
                    if (info.code === 0) {
                        [self.page, self.list] = [1, info.data.list];
                    }
                }).catch(info => {
                    uni.hideLoading();
                    console.error(info);
                });
            },
            address(status) {
                console.log(status);
                this.init();
            },
            manual() {
                uni.navigateTo({
                    url: '/pages/address/address-edit?is_refund_address=' + this.is_refund_address,
                });
            },
            getauto() {
                //#ifndef MP-ALIPAY || H5 || APP-PLUS
                const self = this;
                uni.chooseAddress({
                        success: function (e) {
                            self.$request({
                                url: self.$api.user.wechat_district,
                                data: {
                                    national_code: e.nationalCode,
                                    province_name: e.provinceName,
                                    city_name: e.cityName,
                                    county_name: e.countyName,
                                }
                            }).then(info => {
                                if (info.code === 0) {
                                    let province_id = info.data.district['province']['id'];
                                    let city_id = info.data.district['city']['id'];
                                    let district_id = info.data.district['district']['id'];

                                    let newAddress = {
                                        id: '',
                                        name: e.userName,
                                        ids: [province_id, city_id, district_id],
                                        province_id: province_id,
                                        city_id: city_id,
                                        district_id: district_id,
                                        mobile: e.telNumber,
                                        detail: e.detailInfo,
                                    };

                                    uni.navigateTo({
                                        url: '/pages/address/address-edit?form=' + JSON.stringify(newAddress) + '&is_refund_address=' + self.is_refund_address
                                    })
                                } else {
                                    console.error(info.msg);
                                }
                            });
                        },
                        fail: function (e) {
                            console.error(e);
                        },
                    }
                );
                //#endif
            }
	    }
    }
</script>
<style scoped lang="scss">
    .head {
        background: $uni-weak-color-two;
    }
	.app-buttons {
		width: #{750-24-24rpx};
		height: #{80rpx};
		margin: #{40rpx} #{24rpx} 0;
		.app-icon {
			width: #{32rpx};
			height: #{32rpx};
			background-repeat: no-repeat;
			background-size: 100% 100%;
			margin-right: #{8rpx};
		}
		.app-manual-icon{
			background-image: url("./image/add.png");
		}

        .app-auth-icon {
            background-image: url("./image/auth.png");
		}
		.app-text {
			font-size: #{28rpx};
			color: #ffffff;
			margin-left: #{8rpx};
		}
	}
</style>